package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopDetailBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopDetailProductBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopDetailProjectBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopTypeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.INearShopDetailView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/22.
 */

public class NearShopDetailPresenter extends BasePresenter<INearShopDetailView> {

    public NearShopDetailPresenter(INearShopDetailView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void getShopDetail(String token, String id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).store_detail(token, id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ShopDetailBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<ShopDetailBean> response) {
                        iView.setDetailInfo(response.getData());
                    }
                }));
    }

    public void getFirstShopType(String shop_id, String type) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).type_list(null, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<ShopTypeBean.TypeBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<ShopTypeBean.TypeBean>> response) {
                        iView.setFirstLevel(response.getData());
                        getSecondShopType(type, shop_id, response.getData().get(0).getId(), 1);
                    }
                }));
    }

    public void getSecondShopType(String type, String pid, String typeid, int page) {
        if (type.equals("1")) {
            addSubscription(RetrofitSource.createApi(HttpPostService.class).project_list(pid, typeid, page, 20)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<ResponseBase<ShopDetailProjectBean>>(iView) {
                        @Override
                        public void onSuccess(ResponseBase<ShopDetailProjectBean> response) {
                            iView.setProjectSecondLevel(response.getData());
                        }
                    }));
        } else {
            addSubscription(RetrofitSource.createApi(HttpPostService.class).getProduct(pid, typeid, page, 20)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<ResponseBase<List<ShopDetailProductBean>>>(iView) {
                        @Override
                        public void onSuccess(ResponseBase<List<ShopDetailProductBean>> response) {
                            iView.setProductSecondLevel(response.getData());
                        }
                    }));
        }

    }

    public void collectShop(String token, String store_id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).favor(token, store_id, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.collectSuccess();
                    }
                }));
    }

    public void cancelCollectShop(String token, String store_id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).cancel(token, store_id, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.cancelCollectSuccess();
                    }
                }));
    }

}
